package someTestExcemple.huaweiOd.divideAndRecursion;

//华为OD机试 - 求分数计算结果 - 递归（Java 2025 A卷 100分）
//https://blog.csdn.net/guorui_java/article/details/146798249
import java.math.BigInteger;
import java.util.*;
//todo 完全没看懂
public class CalculateTheResultOfTheFraction {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String expression = sc.nextLine();
        String result = evaluate(expression);
        System.out.println(result);
    }

    private static String evaluate(String expression) {
        try {
            // 清除表达式中的空格
            expression = expression.replaceAll(" ", "");
            // 解析并计算表达式
            Fraction result = parseExpression(expression, new Index());
            // 处理并格式化结果输出
            if (result.denominator.equals(BigInteger.ZERO)) {
                return "ERROR"; // 遇到除数为零
            } else {
                // 化简分数
                result.reduce();
                if (result.denominator.equals(BigInteger.ONE)) {
                    return result.numerator.toString(); // 结果为整数
                } else {
                    return result.numerator + "/" + result.denominator; // 输出最简分数
                }
            }
        } catch (ArithmeticException e) {
            return "ERROR";
        }
    }

    // 分数类
    static class Fraction {
        BigInteger numerator; //分子
        BigInteger denominator; //分母

        Fraction(BigInteger numerator, BigInteger denominator) {
            this.numerator = numerator;
            this.denominator = denominator;
            if (this.denominator.equals(BigInteger.ZERO)) {
                throw new ArithmeticException("Division by zero");
            }
            // 确保分母为正
            if (this.denominator.compareTo(BigInteger.ZERO) < 0) {
                this.denominator = this.denominator.negate();
                this.numerator = this.numerator.negate();
            }
        }

        // 化简分数
        void reduce() {
            if (this.denominator.equals(BigInteger.ZERO)) return;
            BigInteger gcd = this.numerator.gcd(this.denominator);
            if (!gcd.equals(BigInteger.ZERO)) {
                this.numerator = this.numerator.divide(gcd);
                this.denominator = this.denominator.divide(gcd);
            }
        }

        // 加法
        static Fraction add(Fraction a, Fraction b) {
            BigInteger num = a.numerator.multiply(b.denominator).add(b.numerator.multiply(a.denominator));
            BigInteger den = a.denominator.multiply(b.denominator);
            return new Fraction(num, den);
        }

        // 减法
        static Fraction subtract(Fraction a, Fraction b) {
            BigInteger num = a.numerator.multiply(b.denominator).subtract(b.numerator.multiply(a.denominator));
            BigInteger den = a.denominator.multiply(b.denominator);
            return new Fraction(num, den);
        }

        // 乘法
        static Fraction multiply(Fraction a, Fraction b) {
            BigInteger num = a.numerator.multiply(b.numerator);
            BigInteger den = a.denominator.multiply(b.denominator);
            return new Fraction(num, den);
        }

        // 除法
        static Fraction divide(Fraction a, Fraction b) {
            if (b.numerator.equals(BigInteger.ZERO)) {
                throw new ArithmeticException("Division by zero");
            }
            BigInteger num = a.numerator.multiply(b.denominator);
            BigInteger den = a.denominator.multiply(b.numerator);
            return new Fraction(num, den);
        }
    }

    // 用于跟踪当前解析位置
    static class Index {
        int pos = 0;
    }

    // 解析表达式（处理加减）
    private static Fraction parseExpression(String s, Index index) {
        Fraction result = parseTerm(s, index);
        while (index.pos < s.length()) {
            char op = s.charAt(index.pos);
            if (op == '+' || op == '-') {
                index.pos++;
                Fraction term = parseTerm(s, index);
                if (op == '+') {
                    result = Fraction.add(result, term);
                } else {
                    result = Fraction.subtract(result, term);
                }
            } else {
                break;
            }
        }
        return result;
    }

    // 解析项（处理乘除）
    private static Fraction parseTerm(String s, Index index) {
        Fraction result = parseFactor(s, index);
        while (index.pos < s.length()) {
            char op = s.charAt(index.pos);
            if (op == '*' || op == '/') {
                index.pos++;
                Fraction factor = parseFactor(s, index);
                if (op == '*') {
                    result = Fraction.multiply(result, factor);
                } else {
                    result = Fraction.divide(result, factor);
                }
            } else {
                break;
            }
        }
        return result;
    }

    // 解析因子（处理数字和括号）
    private static Fraction parseFactor(String s, Index index) {
        if (s.charAt(index.pos) == '(') {
            index.pos++; // 跳过 '('
            Fraction expr = parseExpression(s, index);
            if (index.pos >= s.length() || s.charAt(index.pos) != ')') {
                throw new ArithmeticException("Mismatched parentheses");
            }
            index.pos++; // 跳过 ')'
            return expr;
        } else {
            // 解析数字
            int start = index.pos;
            while (index.pos < s.length() && Character.isDigit(s.charAt(index.pos))) {
                index.pos++;
            }
            if (start == index.pos) {
                throw new ArithmeticException("Expected number");
            }
            BigInteger num = new BigInteger(s.substring(start, index.pos));
            return new Fraction(num, BigInteger.ONE);
        }
    }
}
